package com.itheima.Wuziqi.ui;

/**
 * 棋盘实现类
 */
public class Chessboard implements Board {
    private int[][] board;
    private int size;
    
    public Chessboard(int size) {
        this.size = size;
        board = new int[size][size];
    }
    
    @Override
    public int getSize() {
        return size;
    }
    
    @Override
    public int getCell(int row, int col) {
        if (row >= 0 && row < size && col >= 0 && col < size) {
            return board[row][col];
        }
        return -1;
    }
    
    @Override
    public boolean placePiece(int row, int col, int player) {
        if (row >= 0 && row < size && col >= 0 && col < size && board[row][col] == 0) {
            board[row][col] = player;
            return true;
        }
        return false;
    }
    
    @Override
    public void reset() {
        board = new int[size][size];
    }
    
    @Override
    public int[][] getBoardState() {
        // 返回棋盘的副本，防止外部直接修改
        int[][] copy = new int[size][size];
        for (int i = 0; i < size; i++) {
            System.arraycopy(board[i], 0, copy[i], 0, size);
        }
        return copy;
    }
}
    